import Vue from 'vue'
import Vuex from 'vuex'
import Axios from 'axios';
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex);

export const store = new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    login: false,
  },
  getters: {
    login: state => {
      return state.login
    }
  },
  mutations: {
    checkLogin: (state, payload) => {
      state.login = payload;
    }
  },
  actions: {
    checkLogin: ({
      commit
    }, payload) => {
      Axios({
        url: 'http://localhost:3000/users/login',
        method: 'post',
        data: {
          name: payload.name,
          password: payload.password
        }
      }).then(res => {
        //console.log(res.data);
        if (res.data) {
          commit('checkLogin', true);
        } else {
          commit('checkLogin', false);
        }
      })
    }
  }
})
